package com.neusoft.elmboot.service.impl;

import com.neusoft.elmboot.mapper.FoodMapper;
import com.neusoft.elmboot.po.Food;
import com.neusoft.elmboot.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class FoodServiceImpl implements FoodService {

    @Autowired
    private FoodMapper foodMapper;

    @Override
    public List<Food> listFoodByBusinessId(Integer businessId) {
        return foodMapper.listFoodByBusinessId(businessId);
    }

    @Override
    public int removeFoodByBusinessIdById(Integer foodId,Integer businessId){
         // 验证该食品是否属于当前商家
         // 根据foodId查询食品信息
        Food food = foodMapper.getFoodById(foodId);

         if (food != null && food.getBusinessId().equals(businessId)) {
            return foodMapper.removeFoodByBusinessIdById(foodId,businessId);
        } else {
            return 0;
        }
    }

    @Override
    public int updateFood(Food food) {
        // 检查要更新的食品是否存在
        Food existingFood = foodMapper.getFoodById(food.getFoodId());
        if (existingFood == null) {
            throw new RuntimeException("Food not found with id: " + food.getFoodId());
        }
        return foodMapper.updateFood(food);
    }
    
    @Override
    public int addFood(Food food) {
        // 验证必要的字段是否存在
        if (food.getFoodName() == null || food.getFoodName().trim().isEmpty()) {
            throw new RuntimeException("Food name cannot be empty");
        }
        if (food.getBusinessId() == null) {
            throw new RuntimeException("Business ID cannot be null");
        }
        
        // 如果没有设置默认图片，设置一个默认图片
        if (food.getFoodImg() == null || food.getFoodImg().trim().isEmpty()) {
            food.setFoodImg("../assets/default_food.png");
        }
        
        // 调用Mapper添加食品
        return foodMapper.addFood(food);
    }
}
